/*
 * Created by JFormDesigner on Sat Feb 27 16:27:26 CST 2016
 */

package cn.zzzhy.client;

import cn.zzzhy.spider.SpiderManager;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author zzzhy zzzhy
 */
public class Client extends JFrame {

    private SpiderManager spiderManager = null;
    private ButtonGroup onlyRadio = new ButtonGroup();

    public Client() {
        spiderManager = new SpiderManager(this);
        initComponents();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public JTextArea getOutText(){
        return outText;
    }

    private void onlyImage(ActionEvent e) {
        System.out.println("onlyImage");
        SpiderManager.fetchType = SpiderManager.FetchType.IMAGE;
    }

    private void onlyMV(ActionEvent e) {
        System.out.println("onlyMV");
        SpiderManager.setFetchType(SpiderManager.FetchType.MV);
    }

    private void allFetch(ActionEvent e) {
        System.out.println("allFetch");
        SpiderManager.setFetchType(SpiderManager.FetchType.All);
    }

    private void startFetch(ActionEvent e) {
        System.out.println("start-fetch");
        setRadioDisable();
        fetchBtn.setEnabled(false);
        String destURL = destURLText.getText();
        spiderManager.start(destURL);
    }

    private void pauseFetch(ActionEvent e) {
        System.out.println("pause-fetch");
        toggleText(pauseBtn);
        spiderManager.pause();
    }

    private void toggleText(JButton pauseBtn) {
        if(pauseBtn.getText().equals("暂停")){
            pauseBtn.setText("继续");
        }else{
            pauseBtn.setText("暂停");
        }
    }

    private void stopFetch(ActionEvent e) {
        System.out.println("stop-fetch");
        spiderManager.stop();
        fetchBtn.setEnabled(true);
        pauseBtn.setEnabled(true);
        pauseBtn.setText("暂停");
    }

    private void setRadioDisable() {
        onlyImageRadio.setEnabled(false);
        onlyMVRadio.setEnabled(false);
        allFetchRadio.setEnabled(false);
    }

    private void setRadioEnable(){
        onlyImageRadio.setEnabled(true);
        onlyMVRadio.setEnabled(true);
        allFetchRadio.setEnabled(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - zzzhy zzzhy
        label1 = new JLabel();
        destURLText = new JTextField();
        scrollPane1 = new JScrollPane();
        outText = new JTextArea();
        fetchBtn = new JButton();
        label2 = new JLabel();
        textPane1 = new JTextPane();
        allURLText = new JTextField();
        invalidURLText1 = new JLabel();
        invalidURLText = new JTextField();
        label4 = new JLabel();
        imageText = new JTextField();
        label5 = new JLabel();
        mvText = new JTextField();
        label6 = new JLabel();
        onlyImageRadio = new JRadioButton();
        onlyMVRadio = new JRadioButton();
        allFetchRadio = new JRadioButton();
        pauseBtn = new JButton();
        stopBtn = new JButton();

        //======== this ========
        setBackground(Color.white);
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u8bf7\u8f93\u5165\u7f51\u5740");

        //======== scrollPane1 ========
        {
            scrollPane1.setToolTipText("\u8fd9\u662f\u7a0b\u5e8f\u8f93\u51fa");
            scrollPane1.setViewportBorder(new LineBorder(Color.black, 2, true));
            scrollPane1.setAutoscrolls(true);
            scrollPane1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            scrollPane1.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
            scrollPane1.setName("outText");
            scrollPane1.setBackground(new Color(204, 204, 204));
            scrollPane1.setBorder(new LineBorder(Color.gray, 1, true));

            //---- outText ----
            outText.setEditable(false);
            outText.setLineWrap(true);
            scrollPane1.setViewportView(outText);
        }

        //---- fetchBtn ----
        fetchBtn.setText("\u641c\u7d22");
        fetchBtn.addActionListener(e -> startFetch(e));

        //---- label2 ----
        label2.setText("\u603b\u94fe\u63a5\u6570");

        //---- allURLText ----
        allURLText.setText("0");
        allURLText.setEditable(false);

        //---- invalidURLText1 ----
        invalidURLText1.setText("\u65e0\u6548\u7684\u94fe\u63a5\u6570");

        //---- invalidURLText ----
        invalidURLText.setText("0");
        invalidURLText.setEditable(false);

        //---- label4 ----
        label4.setText("\u56fe\u7247\u6570");

        //---- imageText ----
        imageText.setText("0");
        imageText.setEditable(false);

        //---- label5 ----
        label5.setText("\u89c6\u9891\u6570");

        //---- mvText ----
        mvText.setText("0");
        mvText.setEditable(false);

        //---- label6 ----
        label6.setText("\u7a0b\u5e8f\u8f93\u51fa");

        //---- onlyImageRadio ----
        onlyImageRadio.setText("\u53ea\u53d6\u56fe\u7247");
        onlyImageRadio.setName("fentchType");
        onlyImageRadio.addActionListener(e -> onlyImage(e));

        //---- onlyMVRadio ----
        onlyMVRadio.setText("\u53ea\u53d6\u89c6\u9891");
        onlyMVRadio.setName("fentchType");
        onlyMVRadio.addActionListener(e -> onlyMV(e));

        //---- allFetchRadio ----
        allFetchRadio.setText("\u5168\u90fd\u53d6\u8d70");
        allFetchRadio.setName("fentchType");
        allFetchRadio.addActionListener(e -> allFetch(e));

        //---- pauseBtn ----
        pauseBtn.setText("\u6682\u505c");
        pauseBtn.addActionListener(e -> pauseFetch(e));

        //---- stopBtn ----
        stopBtn.setText("\u505c\u6b62");
        stopBtn.addActionListener(e -> stopFetch(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(56, 56, 56)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(label6)
                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 761, GroupLayout.PREFERRED_SIZE)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(destURLText, GroupLayout.PREFERRED_SIZE, 570, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addGap(58, 58, 58)
                                    .addComponent(textPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addComponent(label2))
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addGap(43, 43, 43)
                                    .addComponent(onlyImageRadio)
                                    .addGap(18, 18, 18)
                                    .addComponent(onlyMVRadio)
                                    .addGap(18, 18, 18)
                                    .addComponent(allFetchRadio)
                                    .addGap(55, 55, 55)
                                    .addComponent(fetchBtn)
                                    .addGap(18, 18, 18)
                                    .addComponent(pauseBtn)
                                    .addGap(18, 18, 18)
                                    .addComponent(stopBtn))
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addGap(24, 24, 24)
                                    .addComponent(allURLText, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(invalidURLText1)
                                    .addGap(18, 18, 18)
                                    .addComponent(invalidURLText, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(label5)
                                    .addGap(18, 18, 18)
                                    .addComponent(mvText, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(label4)
                                    .addGap(18, 18, 18)
                                    .addComponent(imageText, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)))))
                    .addContainerGap(56, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1)
                        .addComponent(destURLText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(invalidURLText1)
                                .addComponent(invalidURLText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label5)
                                .addComponent(mvText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label4)
                                .addComponent(imageText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(allURLText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label2))
                            .addGap(12, 12, 12)
                            .addComponent(label6))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addGap(22, 22, 22)
                                    .addComponent(textPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addGap(18, 18, 18)
                                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(onlyImageRadio)
                                        .addComponent(onlyMVRadio)
                                        .addComponent(allFetchRadio)
                                        .addComponent(fetchBtn)
                                        .addComponent(pauseBtn)
                                        .addComponent(stopBtn))))
                            .addGap(0, 0, Short.MAX_VALUE)))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 411, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(20, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());

        //---- onlyRadio ----
        onlyRadio.add(onlyImageRadio);
        onlyRadio.add(onlyMVRadio);
        onlyRadio.add(allFetchRadio);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - zzzhy zzzhy
    private JLabel label1;
    private JTextField destURLText;
    private JScrollPane scrollPane1;
    private JTextArea outText;
    private JButton fetchBtn;
    private JLabel label2;
    private JTextPane textPane1;
    private JTextField allURLText;
    private JLabel invalidURLText1;
    private JTextField invalidURLText;
    private JLabel label4;
    private JTextField imageText;
    private JLabel label5;
    private JTextField mvText;
    private JLabel label6;
    private JRadioButton onlyImageRadio;
    private JRadioButton onlyMVRadio;
    private JRadioButton allFetchRadio;
    private JButton pauseBtn;
    private JButton stopBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    public static void main(String[] args) {
        new Client().setVisible(true);
    }
}
